import { Toast } from "@/utils/toast";

export function checkInput<T extends Object>(
  checkTarget: {
    [K in keyof T]: any;
  },
  config: {
    [K in keyof T]?: {
      test: (text: any) => boolean;
      msg: string;
    }[];
  },
  toast?: (t: string) => any,
) {
  return (checkList: (keyof T)[], showToast = true) => {
    return !checkList.some(checkKey => {
      let value = checkTarget[checkKey];
      let checkConfigList = config[checkKey] || [];
      return checkConfigList.some(item => {
        let isTrue = item.test(value);
        if (showToast && !isTrue) {
          toast ? toast(item.msg) : Toast(item.msg);
        }
        return !isTrue;
      });
    });
  };
}
